
import { defineStore } from 'pinia'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import en from 'element-plus/dist/locale/en.mjs'
import zhTw from 'element-plus/dist/locale/zh-tw.mjs'
import request from "@/utils/request";
enum Api {
  selectUrl="/api/ipo/refdata/searchAndFilter",

}
export const ipoStore = defineStore({
  id: 'app',
  state: () => ({
    //1代表英文 2代表繁体中文 3代表简体中文
    locale:1,
    homeDay:new Date(),
    ActiveShow:2,
    mobileActiveShow:1,
    mainExpand:-1,
    filterObj:{},   //把过滤条件放在这里
    keyWord:"",
    selectedArr:{},
    checkedAll:2,
    ipoIds:[] as any,
    newIpo:false
  }),
  getters: {
    localeLang(state) {
      return [en,zhTw,zhCn][state.locale-1];
    },
  },
  actions: {
    //更改语言的方法
    setLocale(data: number) {
      this.locale = data 

    },
    setHomeDay(day:string|Date){
      this.homeDay=day as Date;
    },
    setMobileActiveShow(num:number){
      this.mobileActiveShow=num;
    },
    setActiveShow(num:number){
      this.ActiveShow=num;
    },
    setMainExpand(num:number){
      this.mainExpand=num;
    },
    setFilterObje(obj:any){
      this.filterObj=obj;
    },
   
    setSelectedArr({data,checkedAll,newIpo}:any){
      this.selectedArr=data;
      this.checkedAll=checkedAll;
      this.newIpo=newIpo;
    },
    setKeyWord(keyWord:string){
      this.keyWord=keyWord;
    },
    getSearchSelectList(){
      const keyWord=this.keyWord;
      const selectedArr=this.selectedArr;
      return request.post<any, null>(Api.selectUrl,{keyWord,...selectedArr});
    },
    setIpoIds(ipoId:string|number){
      const arr=this.$state.ipoIds;
      const hasId= arr.some((i:any)=>i==ipoId);
        const index=arr.findIndex((i:any)=>i==ipoId);
        //如果hasid为真，当参数形式为splice(index,1)时表示删除下标为index的内容
        hasId&&arr.splice(index,1)//点击变成不展开后，要把arr的id删除掉,如果不删，点击之后卡片就一直展开了
        !hasId&&arr.push(ipoId)
    },
    isExpand(ipoId:string|number){
      return this.$state.ipoIds.some((i:any)=>i==ipoId);
    }
  },

})